﻿using Nasty.Core.Session;
using Nasty.Core.Entity;
using SqlSugar;
using Nasty.Core.Attributes;
using Newtonsoft.Json;

namespace Nasty.Core.Entity.System
{
    [SugarTable("SysPermission", TableDescription = "权限")]
    public class Permission : StandardEntity<Permission>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        [NastyColumn(ColumnName = "Code", ColumnDescription = "编码", IsRepeat = true)]
        public string? Code { get; set; }


        /// <summary>
        /// 组Id
        /// </summary>
        [SugarColumn(ColumnName = "GroupId", ColumnDescription = "组Id")]
        public string? GroupId { get; set; }

        /// <summary>
        /// 所属分组
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(GroupId))]
        public PermissionGroup? Group { get; set; }

        public override void OnPreAdd()
        {
            base.OnPreAdd();

            var db = AppSession.CurrentDb.Value;
            //var isAny = db.Queryable<PermissionGroup>().Where((t) => t.Code == Code).Any();
            //if (isAny) throw new Exception("编码重复");
        }
    }
}
